<template>
    <course
        v-if="business"
        :key="businessId"
        :counter="counter"
        :business="business"
        :business-template="businessTemplate"
        :apps="apps"
        :mode="mode"
        :action="action"
        :center="true"
        @change="changeBusiness"
    />
</template>
<script>
import { mapState } from 'vuex'
import { Base64 } from 'js-base64'
import course from './course'
import business from './data/business'
import apps from './data/apps'

export default {
    name: 'course-business',
    components: { course },
    data() {
        return {
            businessId: null,
            trainingId: null,
            examId: null,
            examPaperId: null,
            mode: null
        }
    },
    computed: {
        ...mapState('course', ['apps', 'business', 'businessTemplate', 'counter'])
    },
    created() {
        const { course } = this.$route.query
        const query = JSON.parse(Base64.decode(course))
        const { businessId, trainingId, examId, examPaperId, mode, action } = query
        this.businessId = businessId
        this.trainingId = trainingId
        this.examId = examId
        this.examPaperId = examPaperId
        this.mode = mode
        this.action = action
        const { commit, dispatch } = this.$store
        if (trainingId) {
            /**  根据培训id取对应的businessTemplates取值 */
            dispatch('course/getBusinessesByTrainingId', { trainingId, businessId } )
        } else if (examPaperId) {
            /**  根据考试id取对应的考试时长，开始时间 */
            examId && dispatch('getExamById', { examId })
            /**  根据试卷id取对应的businessTemplates取值 */
            examPaperId && dispatch('course/getBusinessesByExamPaperId', { examPaperId, businessId })
        } else if (businessId) {
            dispatch('course/getBusinessById', businessId)
        } else {
            if (!businessId) {
                commit('course/update', { counter: business[1].counter })
                commit('course/update', { business: business[1] })
                commit('course/update', { apps })
            }
        }
    },
    methods: {
        changeBusiness(businessId) {
            this.businessId = businessId
            const { course } = this.$route.query
            const query = JSON.parse(Base64.decode(course))
            const { trainingId, learningId, examId, examPaperId, mode, action } = query
            this.$router.push({
                name: 'courseBusiness',
                query: {
                    course: Base64.encode(
                        JSON.stringify({
                            businessId,
                            trainingId,
                            learningId,
                            examId,
                            examPaperId,
                            mode,
                            action
                        })
                    )
                }
            })
            this.$store.commit('course/changeBusiness', businessId)
        }
    }
}
</script>
